import { aliOssSassUpload } from "@/apis/file";
import useMultipartOssStore from "@/store/modules/multipartOssStore";
import { ElMessage } from "element-plus";

const multipartOssStore = useMultipartOssStore();

const upload = async (options, type, callback) => {
  const { file } = options;
  if (type && !type.includes(file.type)) {
    ElMessage.error("文件格式错误");
    return;
  }
  await multipartOssStore.multipartUpload(`projectmanage/${file.name}`, file, callback);
  await multipartOssStore.setACL(`projectmanage/${file.name}`, "public-read");
  const fileUrl = await multipartOssStore.signatureUrl(`projectmanage/${file.name}`);
  const url = fileUrl.split("?")[0];
  await aliOssSassUpload([
    {
      fileId: file.uid,
      objectName: `projectmanage/${file.name}`,
      url,
      access: "public-read",
      fileName: file.name,
      bucketName: "51-jzt-dev",
      size: file.size,
      type: true,
      suffix: file.name.split(".")[file.name.split(".").length - 1]
    }
  ]);
  return { url, uid: file.uid, fileName: file.name };
};
export default upload;
